const address = [
    {
        id: 1,
        name: "北京市",
        children: [
            { id: 11, name: "海淀区", children: [{ id: 111, name: "中关村" }] },
            { id: 12, name: "朝阳区" },
        ],
    },
    { id: 2, name: "河北省" },
];
// 递归
function find(data, id) {
    let arr = []
    function getNameById(data, id) {
        data.forEach(item => {
            if (item.id == id) {
                arr.push(item.name)
                return
            }
            if (item.children) {
                getNameById(item.children, id)
            }
        })
    }
getNameById(data, 11)
return arr
}

getNameById(address, 11)
    // 河北省 
    ;  // 中关村 
getNameById(address, 32);  // ""

// 利用队列前进
function getNameById(data, id) {
    let queue = []
    for (let item of data) {
        queue.push(item)
        while (queue.length) {
            let top = queue.shift()
            if (top.id == id) {
                return top.name
            }
            if (top.children) {
                queue.push(...top.children)
            }
        }
    }
}
console.log(getNameById(address, 11));

